
#include "audiodialog.h"

AudioDialog::AudioDialog(QWidget *parent, Qt::WFlags f)
    : QWidget(parent, f)
{
	setupUi(this);
	addIcon(rewindPushButton, "rewind.png");
	addIcon(playPushButton, "play.png");
	addIcon(stopPushButton, "stop.png");
	addIcon(forwardPushButton, "forward.png");
	stopPushButton->setEnabled(false);

	audioManager = new AudioManager();
	isPlaying = false;
	pause = false;
	isAudioPlayingListenerEnabled = true;
	interval = 10;
	slider->installEventFilter(this);
}

void AudioDialog::setAudio(QString audioname, int offset)
{
	audionameLabel->setText(audioname);
	audioManager->setAudio(audioname);
	duration = audioManager->getDuration();
	this->offset = offset;
	interval = duration/10;
	slider->setMaximum(duration);	
	totalTimeLabel->setText(timeFormat(duration/10));
	updateView(this->offset);
	audioManager->setOffset(offset);

	connect(audioManager, SIGNAL(audioPlayingSignal(int)),
			this, SLOT(audioPlayingListener(int)));
    connect(audioManager, SIGNAL(audioFinishingSignal()),
			this, SLOT(audioFinishingListener()));
	qDebug() << "total duration" << audioManager->getDuration();
}

void AudioDialog::addIcon(QPushButton *button, QString filename)
{
	QDir dir = QDir::home();
	dir.cd("Documents/icons");
	QPixmap pixmap(dir.filePath(filename)); 
	QIcon icon(pixmap);
    button->setIcon(icon);
}

/**
 * @param time current time position (in second)
 */
QString AudioDialog::timeFormat(int time)
{
	time = time;
	int min = time/60;
	int sec = time%60;
	QString minStr;
	QString secStr;
	if (min < 10) minStr = "0";
	if (sec < 10) secStr = "0";
	minStr += QString::number(min);	
	secStr += QString::number(sec);
	return minStr + ":" + secStr;
}

void AudioDialog::updateView(int offset)
{
	//qDebug() << "update view to" << offset;
	slider->setSliderPosition(offset);
	elapsedTimeLabel->setText(timeFormat(offset/10));
	remainingTimeLabel->setText(timeFormat((duration-offset)/10));	
}

AudioDialog::~AudioDialog()
{
	//audioManager->~AudioManager();
	delete audioManager;
}

void AudioDialog::on_playPushButton_clicked()
{
	qDebug() << "Play...";
	if (isPlaying) {		
		if (pause) {
			pause = false;
			addIcon(playPushButton, "pause.png");
			audioManager->start();
		} else {
			pause = true;
			addIcon(playPushButton, "play.png");
			audioManager->stop();
		}
	} else {
		isPlaying = true;
		audioManager->play(this->offset);
		addIcon(playPushButton, "pause.png");
	}
	stopPushButton->setEnabled(true);
}
void AudioDialog::on_stopPushButton_clicked()
{
	qDebug() << "Stop...";
	stopPushButton->setEnabled(false);
	audioManager->stop();
	audioManager->setOffset(0);
	updateView(0);
	isPlaying = false;
	offset = 0;
	addIcon(playPushButton, "play.png");
	pause = false;
}
void AudioDialog::on_rewindPushButton_clicked()
{
	qDebug() << "Rewind...";
	audioManager->stop();
	slider->setSliderPosition(audioManager->getOffset() - interval);
	on_slider_sliderReleased();
}
void AudioDialog::on_forwardPushButton_clicked()
{
	qDebug() << "Forward...";
	audioManager->stop();
	isAudioPlayingListenerEnabled = false;
	slider->setSliderPosition(audioManager->getOffset() + interval);
	on_slider_sliderReleased();
}
void AudioDialog::on_savePushButton_clicked()
{
	qDebug() << "Save...";
	audioManager->stop();
	isAudioPlayingListenerEnabled = false;
	emit setOffsetSignal(audioManager->getOffset());
	close();
}
void AudioDialog::on_cancelPushButton_clicked()
{
	audioManager->stop();
	qDebug() << "Cancel...";		
	emit cancelSignal();
	close();
}

void AudioDialog::on_slider_valueChanged(int value)
{	
	qDebug()<<"slider value change to"<<value;

	//===== label related
	elapsedTimeLabel->setText(timeFormat(value/10));
	remainingTimeLabel->setText(timeFormat((duration-value)/10));	

	//===== icon related
	if (!isPlaying) {
		isPlaying = true;
		pause = true;
		addIcon(playPushButton, "play.png");		
	}
	if (value == 0) {
		stopPushButton->setEnabled(false);
	} else {	
		stopPushButton->setEnabled(true);
	}		
}
void AudioDialog::on_slider_sliderPressed()
{
	qDebug() << "slider pressed";
	audioManager->stop();
}
void AudioDialog::on_slider_sliderReleased()
{
	qDebug() << "slider released";
	audioManager->setOffset(slider->value());
	isAudioPlayingListenerEnabled = true;
	if (isPlaying && !pause) {
		audioManager->start();
	}
}

void AudioDialog::audioPlayingListener(int time)
{	
	if (isAudioPlayingListenerEnabled && isPlaying) {
		qDebug()<<"AudioDialog::audioPlayingListener("<<time<<")";
		updateView(time);	
	}
}
void AudioDialog::audioFinishingListener()
{
	qDebug() << "catch audioFinishingSignal";
	addIcon(playPushButton, "play.png");
	stopPushButton->setEnabled(false);
	updateView(0);
	isPlaying = false;
	pause = false;
}

bool AudioDialog::eventFilter(QObject *obj, QEvent *event)
{

	if (obj == slider && event->type()==QEvent::MouseButtonPress) {
		qDebug() << "detect mouse press event";
		QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
		qDebug()<<"current pos"<<slider->value();
		audioManager->stop();
		isAudioPlayingListenerEnabled = false;
		slider->setValue(QStyle::sliderValueFromPosition(slider->minimum(),
				slider->maximum(), mouseEvent->x(), slider->width()));
qDebug()<<"move to"<<slider->value();
	}
	return false;

}

void AudioDialog::closeListener()
{
	close();
}




